package com.withoutwords.ui.custom;

import java.util.Arrays;
import java.util.Locale;

import com.withoutwords.engine.Common;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;

public class WordsLayout extends RelativeLayout implements OnClickListener {

	final static int TextSize = 44;
	int highlightedColor;
	int selectedColor;
	int normalColor;
	String[] words;
	int[] txtViewWordIds;
	Context context;
	IWordsLayoutListener listener;
	int targetWidth;
	boolean bShowAllWords;
	int nSelectedIndex = -1;
	int nHighlightedIndex = -1;

	public WordsLayout(Context context) {
		super(context);
		init(context);

	}

	public WordsLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
		// TODO Auto-generated constructor stub
	}

	private void init(Context cn) {
		this.context = cn;
		highlightedColor = Color.YELLOW;
		selectedColor = Color.RED;
		normalColor = Color.WHITE;
		bShowAllWords = false;
	}

	public void createViews(String[] wrds, int width, boolean showAll) {
		bShowAllWords = showAll;
		words = wrds;
		this.targetWidth = width;
		createViews();
	}

	

	

	public void showWord(int index) {
		MyCustomTextView tv = (MyCustomTextView) findViewById(txtViewWordIds[index]);
		tv.setText(words[index]);
		tv.invalidate();
	}

	public void setListener(IWordsLayoutListener l) {
		listener = l;
	}

	static int id = 0x7f06001f;

	// Returns a valid id that isn't in use
	public int findId() {
		View v = findViewById(id);
		while (v != null) {
			v = findViewById(++id);
		}
		return id++;
	}

	void createViews() {

		this.removeAllViews();
		nSelectedIndex = -1;
		nHighlightedIndex = -1;
		txtViewWordIds = new int[words.length];

		MyCustomTextView tv = new MyCustomTextView(context);
		tv.setTextSize(TextSize);
		// tv.setTextAppearance(context, android.R.style.TextAppearance_Medium);
		float[] wordsWidth = new float[words.length];

		// RelativeLayout rly =
		// (RelativeLayout)findViewById(R.id.text_container_id);
		// rly.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
		// LayoutParams.WRAP_CONTENT));
		//
		boolean RTL = Locale.getDefault().getLanguage().toLowerCase()
				.contains("ar");

		float wordSpaces = Common.adjustSize(20, context);
		int tvWordPadding = (int)Common.adjustSize(10, context);
		int tvId = 0;

		int width = targetWidth;
		float nOffset = 0;

		for (int i = 0; i < words.length; i++) {
			wordsWidth[i] = tv.getPaint().measureText(words[i]);
			
			float nEnd = nOffset + wordsWidth[i] + wordSpaces;// 20 for padding;
			boolean newLine = false;
			if (nEnd > width) {
				nOffset = 0;
				newLine = true;
			} else {
				nOffset += wordsWidth[i] + wordSpaces;
			}
			
			MyCustomTextView tvWord = new MyCustomTextView(context);
			tvWord.setTextSize(Common.adjustTextSize(TextSize, context));
			// tvWord.setTextAppearance(context,
			// android.R.style.TextAppearance_Medium);
			tvWord.setId(findId());
			tvWord.setPadding(tvWordPadding, tvWordPadding, tvWordPadding, tvWordPadding);
			tvWord.setTextColor(normalColor);
			
			RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			
			// rlp.addRule(verb, anchor)
			if (i != 0) {
				if (newLine) {
					rlp.addRule(RelativeLayout.BELOW, tvId);
					if (RTL) {
						rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
					} else {

						rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
					}
				} else {
					if (RTL) {
						rlp.addRule(RelativeLayout.LEFT_OF, tvId);
					} else {

						rlp.addRule(RelativeLayout.RIGHT_OF, tvId);
					}

					rlp.addRule(RelativeLayout.ALIGN_TOP, tvId);
				}
			}
			else
			{
				if (RTL) {
					rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
				} else {

					rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
				}
			}

			tvWord.setLayoutParams(rlp);
			if (bShowAllWords) {
				tvWord.setText(words[i]);
			} else {
				char[] ch_ = new char[words[i].length()];
				Arrays.fill(ch_, '_');
				tvWord.setText(new String(ch_));
			}
			tvWord.setOnClickListener(this);
			this.addView(tvWord);

			// save the id for the next run
			tvId = tvWord.getId();
			txtViewWordIds[i] = tvId;

		}

	}

	public void highlightWord(int index) {
		if (txtViewWordIds != null) {
			if (index != nHighlightedIndex) {
				if (nHighlightedIndex != -1) {

					MyCustomTextView tvold = (MyCustomTextView) findViewById(txtViewWordIds[nHighlightedIndex]);
					if (tvold != null) {
						if (nHighlightedIndex == nSelectedIndex) {
							tvold.setTextColor(selectedColor);
						} else {
							tvold.setTextColor(normalColor);
						}
						tvold.invalidate();
					}
				}

				MyCustomTextView tv = (MyCustomTextView) findViewById(txtViewWordIds[index]);
				if (tv != null) {
					tv.setTextColor(highlightedColor);
					nHighlightedIndex = index;
					tv.invalidate();
				}
			}
		}
	}
	
	public void setSelectedWord(int index) {
		if (txtViewWordIds != null) {
			if (index != nSelectedIndex) {
				if (nSelectedIndex != -1) {

					MyCustomTextView tvold = (MyCustomTextView) findViewById(txtViewWordIds[nSelectedIndex]);
					if (tvold != null) {
						
							tvold.setTextColor(normalColor);
						
						tvold.invalidate();
						
					}
				}

				MyCustomTextView tv = (MyCustomTextView) findViewById(txtViewWordIds[index]);
				if (tv != null) {
					tv.setTextColor(selectedColor);
					nSelectedIndex = index;
					tv.invalidate();
				}
			}
		}
	}

	@Override
	public void onClick(View v) {
		
		for (int i = 0; i < txtViewWordIds.length; i++) {
			MyCustomTextView tv = (MyCustomTextView) findViewById(txtViewWordIds[i]);
			if (v.getId() == txtViewWordIds[i]) {
				if (isEnabled()) {
					tv.setTextColor(selectedColor);
					nSelectedIndex = i;
				} else {
					tv.setTextColor(highlightedColor);
					nHighlightedIndex = i;
				}
				listener.onWordPressed(i);
			} else {
				if (i == nSelectedIndex) {
					tv.setTextColor(selectedColor);
				} else if (i == nHighlightedIndex) {
					tv.setTextColor(selectedColor);
				} else {
					tv.setTextColor(normalColor);
				}
			}
		}
		

	}

}
